<template>
    <div class="login_container">
        <el-row>
            <el-col :span="12" :xs="0"></el-col>
            <el-col :span="12" :xs="24">
                <el-form class="login_form" :model="loginForm" :rules="rules" ref="loginForms">
                    <h1>Hello</h1>
                    <h2>欢迎访问</h2>
                    <el-form-item style="margin: 20px 0 ;" prop="username">
                        <el-input :prefix-icon="User" v-model="loginForm.username"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input type="password" :prefix-icon="Lock" v-model="loginForm.password" show-password></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" class="login_btn" :loading="longinStatus" size="default"
                            @click="login()">登录</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </div>
</template>
<script setup lang='ts'>
import { ref, reactive } from 'vue'
import { User, Lock } from '@element-plus/icons-vue';
import { useRouter, useRoute } from 'vue-router';
//导入用户仓库
import useUserStore from '@/store/modules/user';
import { ElNotification } from 'element-plus';
//引入获取当前时间段的函数
import getTime from "@/utils/time"

let route = useRoute()
let $router = useRouter()
let userStore = useUserStore();
let longinStatus = ref(false)
//获取组件实例
let loginForms = ref()
//收集账号与密码的数据
let loginForm = reactive({ username: 'admin', password: 'atguigu123' })
//登录按钮的回调函数
const login = async () => {
    // 保证表单校验通过才发起请求
    await loginForms.value.validate();
    longinStatus.value = true;
    //点击登录之后的行为
    //1.通知仓库发起请求
    //2.请求成功时跳转首页并展示数据
    //3.请求失败时弹出登录失败的信息
    try {
        //保证登录成功
        await userStore.userLogin(loginForm);
        longinStatus.value = false;
        let redirect = route.query.redirect as string;
        $router.push({ path: redirect || '/' })
        //登录成功的提示信息
        ElNotification({
            type: 'success',
            message: '欢迎回来',
            title: `Hi,${getTime()}好`
        })
    }
    catch (error) {
        //登录失败的提示信息
        ElNotification({
            type: 'error',
            message: (error as Error).message
        })
    }
    longinStatus.value = false;
}
const validatorUserName = (rule: any, value: any, callback: any) => {
    //rule:即为规则的校验对象
    //value即是表单的文本内容
    //函数:如果符合条件 callback放行通过即可
    //     如果不符合条件callback方法.则注入错误信息
    if (value.length >= 5) {
        callback();
    } else {
        callback(new Error('账号长度为5~10位'))
    }
}
const validatorPassword = (rule: any, value: any, callback: any) => {
    if (value.length >= 6) {
        callback();
    } else {
        callback(new Error('密码长度为5~10位'))
    }
}
//定义表单校验需要的配置对象
/**
 * required 是否校验该字段
 * min/max 文本最小/大长度
 * message 消息提示内容
 * trigger 触发时机
 */
const rules = {
    username: [
        // { required: true, message: '用户名不能为空', trigger: 'blur' },
        { trigger: 'change', validator: validatorUserName },
        // { required: true, min: 4, max: 10, message: '用户名长度为4~10位', trigger: 'blur' },
    ],
    password: [
        // { required: true, min: 6, max: 15, message: '密码长度为6~15位', trigger: 'change' }
        { validator: validatorPassword }
    ]
}
</script>
<style lang='scss' scoped>
* {
    margin: 0;
    padding: 0;
    border: 0;
}

.login_container {
    width: 100%;
    height: 100vh;
    background-image: url("@/assets/images/background.jpg");
    background-size: cover;

    .login_form {
        position: relative;
        width: 80%;
        top: 30vh;
        padding: 40px;
        background-image: url("@/assets/images/login_form.png");
        background-size: cover;

        h1 {
            color: white;
            font-size: 40px;
        }

        h2 {
            font-size: 20px;
            color: white;
            margin: 20px 0;
        }

        .login_btn {
            margin-top: 20px;
            width: 100%;
        }

    }
}
</style>